facet_wrap(scales="free_y") not producing desired result
NickName:user2808302 Ask DateTime:2016-12-01T09:38:30

facet_wrap(scales="free_y") not producing desired result

I am trying to create a plot using ggplot2 with 9 subplots. My goal is to allow the vertical axis to be scaled at the individual level for each of the 9 subplots. To that end, I am trying the syntax facet_wrap(scales = "free_y"). However, it does not appear to be working for me, and instead, the vertical axis seems to be identical across all 9 subplots. Below is a MWE:

x <- rep(c(rep("F",4),rep("M",4)),9)
y <- abs(c(rnorm(36,0,20),rnorm(36,0,5)))
z <- rep(1:9,each=8)
meanG1 <- c()
meanG2 <- c()
for (i in c(1,9,17,25,33,41,49,57,65)){
  meanG1 <- c(meanG1, rep(mean(y[i:(i+3)]),8))
  meanG2 <- c(meanG2, rep(mean(y[5:(i+3)]),8))
}
dat <- data.frame(x=x,y=y,z=z,meanG1=meanG1,meanG2=meanG2)

ggplot(dat, aes(x, y)) + 
  geom_point(aes(colour = factor(x)), shape = 20, size=5, alpha = 0.5) + 
  scale_shape(solid = FALSE) + 
  ggtitle(paste("My Plot")) + 
  ylab("Count") + 
  scale_y_continuous(limits=c(0, max(dat$y))) + 
  theme(axis.title.x = element_blank(), 
        legend.position="bottom", 
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12), 
        legend.title=element_text(size=12), 
        legend.text=element_text(size=12), 
        plot.title=element_text(hjust=0.5)) + 
  labs(colour = "Group", size=12) + 
  geom_segment(aes(x = 1, y = meanG1, xend = 2, yend = meanG2), colour="gray25", size = 0.1) + 
  facet_wrap(~ z, ncol = 3, scales = "free_y")

If anyone has advice on what might be causing this and/or how to fix this, I would be all ears! Thank you.

Copyright Notice:Content Author:「user2808302」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901236/facet-wrapscales-free-y-not-producing-desired-result

More about “facet_wrap(scales="free_y") not producing desired result” related questions

Ggplotly corrupts facet_wrap() fixed scales

When I plot this using only ggplot2, with fixed scales, there's no problem at the faceted plot ggplot(aes(x = fecha, y = prom_imagen_pos)) + geom_line(size = 1.5) + annotate(geom='line', x=

Show Detail

unwanted axis w/ facet_wrap with scales = "free_x"

Maybe this is a duplicate not sure (LINK). I want to use coord_flip, facet_wrap and the argument scales = "free". If one does so this adds unwanted scales in between the facets that are unnecessa...

Show Detail

Unexpected plot using xlim() and facet_wrap(..., scales="free_y" )

I have a problem that can be reproduced using the mt data set: require(ggplot2) mt &lt;- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() mt + facet_wrap(~ cyl, scales = "free_y")...

Show Detail

ggplot 2: Combining free and fixed scales among facet_wrap panels

When plotting multiple panels using facet_wrap, is it possible to allow scales to be free between rows of panels (but fixed among the columns within each row of panels)? In other words, in the

Show Detail

Error plotting chart with facet_wrap and scales = "free" in plotly

I have a time series with various categories (facets), and I am trying to create a chart using the facet_wrap feature from ggplot2 and upload it on plotly. If I set scales = "fixed", I have no pr...

Show Detail

Scales = "free" works for facet_wrap but doesn't for facet_grid

I'm trying to understand why the outputs of facet_grid() and facet_wrap() are different, even though the inputs are the same: facet_grid ggplot(temp, aes(x = valor)) + geom_histogram(binwidth = 5...

Show Detail

Scales = "free" works for facet_wrap but doesn't for facet_grid

I'm trying to understand why the outputs of facet_grid() and facet_wrap() are different, even though the inputs are the same: facet_grid ggplot(temp, aes(x = valor)) + geom_histogram(binwidth = 5...

Show Detail

ggplot - geom_text with free scales facet_wrap

I have the following data: thedata &lt;- data.frame(value= c(90,100,2,10) ,category1 = c("A","A","B","B") ,category2 = c("C","D"

Show Detail

Change y limits in ggplot with facet_wrap to mix of log and regular scales

I have a dataset that has a wide range of values for one group. Using ggplot's facet_wrap, I would plot the y axis in a log scale for one group (the group that has the widest range of values) and r...

Show Detail

Individual axes labels in facet_wrap without scales="free"

My data looks like this: df &lt;- data.frame(Year = as.factor(c(rep(2015, 3), rep(2016, 3), rep(2017,3))), Tax = as.factor(c(rep(c("A", "B", "C"), 3))), Depth = as.

Show Detail